In these exercises you must use the notes above to come up with answers to the questions below.
I want to hold the following, suggest sensible variable types for them:
- Number of chickens in the hen house
- Average weight of each chicken
- Speed of the car in miles per hour
- Price in pence of the ice cream
-
Which of the following are not sensible variable names, and why:
count
2sensor
car_speed
engine-size
x
-
What would be the result of the following expressions:
2 + 2
2 + 3 * 5
(2+3) * 4
5 & 8
4 | 5
5 ^ 7
5 ^ 2
'A' + 3
7 &< &< 1
3 >> 2
10 % 3
Explain the value that the variables would have after the following C had been obeyed:
/* equation A */ int i, result ; i = 0 ; result = i + 1 ;
/* equation B */ int i = 1, result ; result = ++i ;
/* equation C */ float x ; x = 7 / 2 ;
/* equation D */ float x ; int i ; x = 7.0 / 2.0 ; i = x ;
-
I would use the following :
- An integer type would be appropriate here, unless we are absolutely sure that we would never have more than 256 chickens in the hen house
- A floating point type may be appropriate, but if we are using a weight to a particular number of grams(i.e. I have imposed limits on my resolution) then an integer or char may be more appropriate
- Again, we would probably think to use a floating point type, but we could probably use an integer or a char
- I'd use an integer type for this
- Perfectly acceptable, I use names like this all the time
- Not legal because it starts with a digit
- Not legal because the '-' charactor is not allowed
- Legal, but stupid unless the reader of your program knows precisely what x means
- 2 + 2 = 4 [This one should be easy]
- 2 + 3 * 5 = 17 [The multiplication is done first, so this is 2 + ( 3 * 5 ) = 2 + 15 = 17]
- (2+3)*4 = 20 [We change the order of the calculation using brackets, so we get 4 * 5 = 20]
- 5 & 8 = 0 [If we look at 5 in binary, we get 0101, if we look at 8 we get 1000. If you and these together, you get 0000, which is 0]
- 4 | 5 = 5 [4 in binary is 0100, 5 is 0101, if we OR these together, you get 0101 as a result, which is 5]
- 5 ^ 7 = 2 [Remember the eXclusive OR says we get 0 if both the bits are the same. 5 in binary is 0101, and 7 is 0111. The only bit different is bit number 1 (remember we count from 0), which leads to 0010 = 2 as a result]
- 5 ^ 2 = 7 [We can again solve this by converting to binary and looking at each bit in turn. We get 0111 as a result, which is 7. Note that this is the same as the value we started with in the previous example, this is because if we XOR something with the same value twice, we get back where we started]
- 'A' + 3 = 'D' [This is perfectly OK, in that we can combine constants like this, and would get the value that is the ASCII code for 'D']
- 7 >> 1 = 3 [This halves the value 7 and throws away the remainder]
- 3 << 2 = 12 [Remember, each time we shift right we double the number, so shifting it to the left twice doubles it twice, i.e. 0011 goes to 1100, which is 12]
- 10 % 3 = 1 [The remainder (or modulus) operator gives you the remainder left over when you divide. 10 divided by 3 leaves remainder 1, so the result of the expression is 1]
-
- /* equation A */
int i, result ;
i = 0 ;
result = i + 1 ;
This would leave result holding 1, the answer to the calculation i + 1, and i holding 0
- /* equation B */
int i = 1, result ;
result = ++i ;
This would result in result holding 2 and i holding 2. The ++ operator placed before the variable name means 'apply the operation before the value is used in another expression'. Please don't write code like this as it is very hard to understand.
- /* equation C */
float x ;
x = 7 / 2 ;
Unexpectedly, or not, x is set to 3. This is because the compiler treats 7/2 as an integer operation between two integer operads. When we do integer sums, we throw away the fractional part, so the 3.5 result is made into 3.
- /* equation D */
float x ;
int i ;
x = 7.0 / 2.0 ;
i = x ;
Because 7.0/2,0 is a floating point expression, the value in x is set to a floating point result, 3.5. However, when this fractional result is transferred to i the fractional part is discarded leaving 3.
- /* equation A */
Answers